package com.ruoyi.park.service.impl;

import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.park.domain.WyClean;
import com.ruoyi.park.domain.WyRequisitionDetail;
import com.ruoyi.park.mapper.WyCleanMapper;
import com.ruoyi.park.mapper.WyRequisitionDetailMapper;
import com.ruoyi.park.service.IWyCleanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 保洁管理Service业务层处理
 *
 * @author cjie
 * @date 2021-07-22
 */
@Service
public class WyCleanServiceImpl implements IWyCleanService {
    @Autowired
    private WyCleanMapper wyCleanMapper;
    @Autowired
    private WyRequisitionDetailMapper wyRequisitionDetailMapper;

    /**
     * 查询保洁管理
     *
     * @param id 保洁管理ID
     * @return 保洁管理
     */
    @Override
    public WyClean selectWyCleanById(String id) {
        WyClean wyClean = wyCleanMapper.selectWyCleanById(id);

        WyRequisitionDetail queryDetail = new WyRequisitionDetail();
        queryDetail.setLinkId(id);
        wyClean.setRequisitionDetailList(wyRequisitionDetailMapper.selectWyRequisitionDetailList(queryDetail));
        return wyClean;
    }

    /**
     * 查询保洁管理列表
     *
     * @param wyClean 保洁管理
     * @return 保洁管理
     */
    @Override
    public List<WyClean> selectWyCleanList(WyClean wyClean) {
        return wyCleanMapper.selectWyCleanList(wyClean);
    }

    /**
     * 新增保洁管理
     *
     * @param wyClean 保洁管理
     * @return 结果
     */
    @Override
    public int insertWyClean(WyClean wyClean) {
        wyClean.setId(IdUtil.getSnowflake(0, 0).nextIdStr());
        wyClean.setCreateTime(DateUtils.getNowDate());
        wyClean.setCreateBy(SecurityUtils.getUsername());

        insertRequisitionDetail(wyClean);
        return wyCleanMapper.insertWyClean(wyClean);
    }

    /**
     * 插入领料详情
     * @param wyClean 清洁管理
     */
    private void insertRequisitionDetail(WyClean wyClean) {
        List<WyRequisitionDetail> requisitionDetailList = wyClean.getRequisitionDetailList();
        if(requisitionDetailList != null && requisitionDetailList.size()>0){
            requisitionDetailList.forEach(wyRequisitionDetail ->{
                wyRequisitionDetail.setId(IdUtil.getSnowflake(0, 0).nextIdStr());
                wyRequisitionDetail.setLinkId(wyClean.getId());

                wyRequisitionDetailMapper.insertWyRequisitionDetail(wyRequisitionDetail);
            });
        }
    }

    /**
     * 修改保洁管理
     *
     * @param wyClean 保洁管理
     * @return 结果
     */
    @Override
    public int updateWyClean(WyClean wyClean) {
        wyClean.setUpdateTime(DateUtils.getNowDate());
        wyClean.setUpdateBy(SecurityUtils.getUsername());

        List<WyRequisitionDetail> requisitionDetailList = wyClean.getRequisitionDetailList();
        if(requisitionDetailList != null && requisitionDetailList.size()>0){
            wyRequisitionDetailMapper.deleteDetailByLinkId(wyClean.getId());

            insertRequisitionDetail(wyClean);
        }
        return wyCleanMapper.updateWyClean(wyClean);
    }

    /**
     * 批量删除保洁管理
     *
     * @param ids 需要删除的保洁管理ID
     * @return 结果
     */
    @Override
    public int deleteWyCleanByIds(String[] ids) {
        return wyCleanMapper.deleteWyCleanByIds(ids);
    }

    /**
     * 删除保洁管理信息
     *
     * @param id 保洁管理ID
     * @return 结果
     */
    @Override
    public int deleteWyCleanById(String id) {
        return wyCleanMapper.deleteWyCleanById(id);
    }
}
